package LInkedList;

public class MyLinkedList implements IList{

    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val){
            this.val=val;
        }
    }

    public void creatList(){
        ListNode node1=new ListNode(12);
        ListNode node2=new ListNode(23);
        ListNode node3=new ListNode(34);
        ListNode node4=new ListNode(45);
        ListNode node5=new ListNode(56);
        ListNode node6=new ListNode(67);

        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        node4.next=node5;
        node5.next=node6;

        this.head=node1;
    }

    public ListNode head;
    @Override
    public void addFirst(int data) {
        ListNode newH=new ListNode(data);
        if (head==null){
            head=newH;
        }else {
            newH.next=head;
            this.head=newH;
        }

    }

    @Override
    public void addLast(int data) {
        ListNode newL=new ListNode(data);
        if(head==null){
            this.head=newL;
        }else {
            ListNode cur=this.head;
            while (cur.next!=null){
                cur=cur.next;
            }
            cur.next=newL;
        }

    }

    @Override
    public void addIndex(int index, int data) throws IndexLegal{
        if(index<0||index>size()){
            throw new IndexLegal("index格式不正确");
        }
        if(index==0){
            addFirst(data);
            return;
        }
        if (index==size()){
            addLast(data);
            return;
        }
        ListNode cur=searchPrev(index);
        ListNode newH=new ListNode(data);
        newH.next=cur.next;
        cur.next=newH;

    }
    public ListNode searchPrev(int index){
        ListNode cur=this.head;
        while (index-1!=0){
            cur=cur.next;
            index--;
        }
        return cur;
    }
    @Override
    public boolean contains(int key) {
       ListNode cur=this.head;
       while (cur.next!=null){
           if(cur.val==key){
               return true;
           }
           cur=cur.next;
       }
       return false;
    }

    public ListNode findPrev(int key){
        ListNode cur=this.head;
        while (cur.next!=null){
            if (cur.next.val==key){
                return cur;
            }
            cur=cur.next;
        }
        return null;

    }
    @Override
    public void remove(int key) {
        if(this.head == null) {
            //一个节点都没有 无法删除！
            return;
        }
        if(this.head.val == key) {
            this.head = this.head.next;
            return;
        }
        //1. 找到前驱
        ListNode cur = findPrev(key);
        //2、判断返回值是否为空？
        if(cur == null) {
            System.out.println("没有你要删除的数字");
            return;
        }
        //3、删除
        ListNode del = cur.next;
        cur.next = del.next;
    }

    @Override
    public void removeAllKey(int key) {

    }

    @Override
    public int size() {
        ListNode cur=this.head;
        int count=0;
        while (cur.next!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }

    @Override
    public void clear() {

    }

    @Override
    public void display() {
        ListNode cur=this.head;
        while (cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }
}
